package ihm;

//usage : java ihm.Coord fichiermage.jpg

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.ScrollPane;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Coord extends JFrame {
	private static final long serialVersionUID = 1L;
	static final int DELTA = 20;
	int hauteurPlan, largeurPlan;
	// pour la vue scrollable de l'image
	protected ImageCanvas canvas = new ImageCanvas();
	protected ScrollPane mapView = new ScrollPane();
	// pour les messages et interactions avec l'utilisateur
	protected PanelDeMessages msgView = new PanelDeMessages();

	public Coord(String fichierImage) {
		// chargement de l'image
		Image im = new ImageIcon(fichierImage).getImage();
		hauteurPlan = im.getHeight(null);
		largeurPlan = im.getWidth(null);
		// preparation de la vue scrollable de l'image
		canvas.setImage(im);
		canvas.addMouseListener(msgView);
		mapView.setSize(hauteurPlan + DELTA, largeurPlan + DELTA);
		mapView.add(canvas);
		// construction de l'ensemble
		setLayout(new BorderLayout());
		add(mapView, BorderLayout.CENTER);
		add(msgView, BorderLayout.SOUTH);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				System.exit(0);
			}
		});
	}

	// classe locale de canvas
	class ImageCanvas extends Canvas {
		/**
	 * 
	 */
		private static final long serialVersionUID = 1L;
		Image image;

		void setImage(Image img) {
			image = img;
			setSize(largeurPlan, hauteurPlan);
			repaint();
		}

		public void paint(Graphics g) {
			// image
			// if (image!=null)
			// g.drawImage(image,DELTA,DELTA,hauteurPlan,largeurPlan,this);
			if (image != null)
				g.drawImage(image, DELTA, DELTA, this);
			// cadrillage
			int lignes = hauteurPlan / DELTA;
			int colonnes = largeurPlan / DELTA;
			g.setColor(Color.gray);
			for (int i = 1; i <= lignes; i++) {
				g.drawString("" + i, 0, (i + 1) * DELTA);
				g.drawLine(DELTA, i * DELTA, DELTA + largeurPlan, i * DELTA);
			}
			g.drawLine(DELTA, (lignes + 1) * DELTA, DELTA + largeurPlan,
					(lignes + 1) * DELTA);
			for (int i = 1; i <= colonnes; i++) {
				g.drawString("" + i, i * DELTA, DELTA / 2);
				g.drawLine(i * DELTA, DELTA, i * DELTA, DELTA + hauteurPlan);
			}
			g.drawLine((colonnes + 1) * DELTA, DELTA, (colonnes + 1) * DELTA,
					DELTA + hauteurPlan);
		}
	}

	// classe locale pour la vue des messages
	class PanelDeMessages extends JPanel implements MouseListener {
		/**
	 * 
	 */
		private static final long serialVersionUID = 1L;
		JLabel xview = new JLabel();
		JLabel yview = new JLabel();

		PanelDeMessages() {
			add(new JLabel("Clic on the map to show coordinates:"));
			add(new JLabel("x="));
			add(xview);
			add(new JLabel("y="));
			add(yview);
		}

		// protocole de MouseListener
		// released => selection de coordonnees
		// le reste => rien a faire
		public void mouseReleased(MouseEvent e) {
			int x = e.getX() / DELTA;
			int y = e.getY() / DELTA;
			xview.setText("" + x);
			yview.setText("" + y);
		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseClicked(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}
	}

	// lancement de l'IHM
	public static void main(String argv[]) {
		if (argv.length != 1)
			System.err.println("usage : java ihm.Coord fichiermage.jpg");
		else {
			Coord w = new Coord(argv[0]);
			w.setTitle("Coordinates");
			w.setSize(600,500);
			w.setVisible(true);
		}
	}

}
